package com.tesco.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tesco.dao.YgOrderDao;
import com.tesco.entity.domin.YgGoods;
import com.tesco.entity.domin.YgOrder;
import com.tesco.service.YgGoodsService;
import com.tesco.service.YgOrderService;
import com.tesco.utils.ConstantUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class YgOrderServiceImpl extends ServiceImpl<YgOrderDao, YgOrder> implements YgOrderService {


    @Autowired
    private YgGoodsService ygGoodsService;

    @Override
    public Boolean addOrderForGood(String username, int spid) {
        YgOrder ygOrder = new YgOrder();
        ygOrder.setSpid(spid);
        ygOrder.setCreatedat(new Date());
        ygOrder.setUsername(username);
        ygOrder.setIsdeliver(ConstantUtils.OrderStatus3);
        ygOrder.setSjwc(ConstantUtils.OrderStatus1);
        //默认1
        ygOrder.setSpnum(1);
        return this.save(ygOrder);
    }

    @Override
    public List<YgOrder> userGoodList(String username) {
        QueryWrapper<YgOrder> ygOrderQueryWrapper = new QueryWrapper<>();
        ygOrderQueryWrapper.eq(YgOrder.COL_USERNAME,username)
        .orderByDesc(YgOrder.COL_CREATEDAT);
        return this.list(ygOrderQueryWrapper);
    }

    @Override
    public List<YgOrder> deliveryGoodList(String username) {
        QueryWrapper<YgOrder> ygOrderQueryWrapper = new QueryWrapper<>();
        ygOrderQueryWrapper.eq(YgOrder.COL_SHYNAME,username)
                .orderByDesc(YgOrder.COL_CREATEDAT);
        return this.list(ygOrderQueryWrapper);
    }

    @Override
    public List<YgOrder> businessGoodList(String username) {
        List<YgGoods> goodInfoList = ygGoodsService.getGoodInfoList(username);
        List<Integer> spids = goodInfoList.stream().map(a -> a.getSpid()).collect(Collectors.toList());
        QueryWrapper<YgOrder> ygOrderQueryWrapper = new QueryWrapper<>();
        ygOrderQueryWrapper.in(YgOrder.COL_SPID,spids)
                .orderByDesc(YgOrder.COL_CREATEDAT);
        return this.list(ygOrderQueryWrapper);
    }

    @Override
    public YgOrder getOrderInfo(int ddid) {
        YgOrder byId = this.getById(ddid);
        return byId;
    }
}

